package kr.co.medical.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import kr.co.medical.dao.MedicalDao;
import kr.co.medical.dao.PatientDao;
import kr.co.medical.dto.CheobangTempInsert;
import kr.co.medical.dto.Radio_Info;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MedicalController {
	@Autowired
	private SqlSession sqlSessionTemplate;
	
	@RequestMapping("mwaiting.medic")
	public ModelAndView m_waitinglist(ModelAndView mav,HttpSession session) throws ClassNotFoundException, SQLException{
		MedicalDao medicalDao = sqlSessionTemplate.getMapper(MedicalDao.class);
		List<Radio_Info> list = medicalDao.selectRadio();
		
		mav.setViewName("medical/m_waiting_list");
		String medical = (String) session.getAttribute("medical");
		mav.addObject("medical", medical);
		mav.addObject("radiolist", list);
		
		return mav;
	}
	@RequestMapping(value={"fileupload.medic"}, method=RequestMethod.POST)
	public void fileUpload(CheobangTempInsert cheobangTI, HttpServletRequest request, HttpServletResponse res) throws IOException, ClassNotFoundException, SQLException{

		CommonsMultipartFile files = cheobangTI.getFile();
		System.out.println(cheobangTI.getFile());
		System.out.println("files.getOriginalFilename()1 : " + files.getOriginalFilename());
		List<String> fileNames = new ArrayList<String>();

		if(files != null){
			//이 경우라면 최소 한개는 파일 첨부
			String fname = files.getOriginalFilename();
			System.out.println("fname : " + fname);
			String path = request.getRealPath("resources/images/upload");
			System.out.println("path : " + path);
			String fullpath = path + "\\" + fname;
			System.out.println("fullpath : " + fullpath);
			
			if(!fname.equals("")){
				FileOutputStream fs = new FileOutputStream(fullpath);
				fs.write(files.getBytes());
				fs.close();
				fileNames.add(fname); //파일 이름만 별도로 관리
			}
		}
		
		if(fileNames.size() > 0){
			cheobangTI.setAddfile(fileNames.get(0));
			fileNames.clear();
		}else{
			cheobangTI.setAddfile("NOT EMPTY");
		}
		
		System.out.println("cheobangTI.getAddfile() : " + cheobangTI.getAddfile());
		
		String addfile = cheobangTI.getAddfile();
		String jupsoo_id = cheobangTI.getJupsoo_id();
		String cheobang_temp_id = cheobangTI.getCheobang_temp_id();
		System.out.println("Cheobang_Temp_Id : " +cheobang_temp_id );
		System.out.println(jupsoo_id);
		MedicalDao medicaldao = sqlSessionTemplate.getMapper(MedicalDao.class);
		int i = medicaldao.updateAddfile(cheobangTI);
		
		if(i>0){
			System.out.println("성공했다규!!");
			System.out.println("삭제될 Cheobang_Temp_Id : " + cheobang_temp_id);
			medicaldao.addwaiting2(jupsoo_id);
			medicaldao.AddfileuserDelete(cheobang_temp_id);
			System.out.println("삭제됨");
			res.sendRedirect("mwaiting.medic");
		}
	}
}
